package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;


public class UserServiceImpl implements UserService {
    @Override
    public PageInfo<User> findAll(int currentPage, int pageSize) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao dao = sqlSession.getMapper(UserDao.class);
        //1. 查询之前, 设置分页参数
        PageHelper.startPage(currentPage,pageSize);

        List<User> users = dao.findAll();

        //2. 封装PageInfo<User>对象
        PageInfo<User> pageInfo = new PageInfo<User>(users);

        sqlSession.close();
        return pageInfo;
    }

    @Override
    public void save(User user) {
        UserDao userDao= (UserDao) MybatisUtils.getMapper(UserDao.class);
        userDao.save(user);

       /* SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtils.getSqlSession();
            UserDao dao = sqlSession.getMapper(UserDao.class);

            dao.save(user);

            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            sqlSession.close();
        }
*/
    }

    @Override
    public void deleteById(String id) {
        UserDao userDao= (UserDao) MybatisUtils.getMapper(UserDao.class);
        userDao.deleteById(id);
      /*  SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtils.getSqlSession();
            UserDao dao = sqlSession.getMapper(UserDao.class);
            dao.deleteById(id);

            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            sqlSession.close();
        }*/
    }

    @Override
    public User findById(String id) {
        UserDao userDao= (UserDao) MybatisUtils.getMapper(UserDao.class);
        User user = userDao.findById(id);
        return user;
      /*  SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao dao = sqlSession.getMapper(UserDao.class);
        User user = dao.findById(id);
        sqlSession.close();
        return user;*/
    }

    @Override
    public void update(User user) {
        UserDao userDao= (UserDao) MybatisUtils.getMapper(UserDao.class);
        userDao.update(user);


/*        SqlSession sqlSession = MybatisUtils.getSqlSession();
    UserDao dao = sqlSession.getMapper(UserDao.class);
        dao.update(user);
        sqlSession.commit();
        sqlSession.close();*/
}

    @Override
    public User login(User user) {
        UserDao userDao= (UserDao) MybatisUtils.getMapper(UserDao.class);
        User u = userDao.login(user.getUsername(), user.getPassword());

      /*  SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao dao = sqlSession.getMapper(UserDao.class);
        User u = dao.login(user.getUsername(), user.getPassword());
        sqlSession.commit();
        sqlSession.close();*/
        return u;

    }

    @Override
    public void deleteAll(String[] ids) {
        UserDao userDao= (UserDao) MybatisUtils.getMapper(UserDao.class);
        userDao.deleteAll(ids);
    }

    @Override
    public int findByName(String name) {
        UserDao userDao = (UserDao) MybatisUtils.getMapper(UserDao.class);
        return userDao.findByName(name);
    }

/*    @Override
    public int findByName(String name) {
        UserDao userDao= (UserDao) MybatisUtils.getMapper(UserDao.class);
       return userDao.findByName(name);
    }*/
}
